<?PHP
/**
 * 企业信息管理模型
 * User: yuanji
 * Date: 2017年12月29日
 */
namespace SDF\Lib\Account;

use MongoDB\Driver\WriteConcern;
use SDF\Db\MongoDb;

/**
 * 账户中心，用户管理类
 * @package SDF\Lib\Account
 */
class enterprise extends MongoDb {
    /**
     * 授权构造
     * special_auth constructor.
     */
    public function __construct(){
        parent::__construct('enterprise', 'account');
    }

    /**
     * 添加企业信息
     * @param $data
     * @param array $options
     * @param WriteConcern|NULL $writeConcern
     * @return array|\MongoDB\Driver\WriteResult
     * @throws \Exception
     */
    public function insertOne(&$data,array $options = [], WriteConcern $writeConcern = NULL) {
        if(empty($data['enterprise_name'])){
            throw new \Exception('enterprise_name is empty');
        }
        if(empty($data['identifier'])){
            throw new \Exception('identifier is empty');
        }
        $enterprise_data = array(
            'enterprise_name' => $data['enterprise_name'],//企业名称
            'identifier' => $data['identifier'],//统一社会信用代码（Unified Social Credit Identifier）
            'legal_representative' => $data['legal_representative'],//法定代表人
            'registered_type' => $data['registered_type'],//注册类型
            'registered_capital' => $data['registered_capital'],//注册资本
            'registered_date' => $data['registered_date'],//成立日期
            'registration_authority' => $data['registration_authority'],//登记机关
            'registration_status' => $data['registration_status'],//登记状态
            'area' => $data['area'],//所在区域
            'registered_address' => $data['registered_address'],//注册地址
            'scope_business' => $data['scope_business'],//经营范围
            'create_time' => time(),//录入时间
            'enterprise_status' => 0,//状态，0：待审核、1：已通过、-1：未通过
            'logo' => '',//头像
            'contact_info'=>array(
                'contact_name'=>$data['contact_name'],//联系人姓名
                'id_card_number'=>$data['id_card_number'],//身份证号码
                'phone'=>$data['phone'],//联系电话
                'email'=>$data['email'],//邮箱
            ),
        );
        parent::insertOne($enterprise_data);
        return $enterprise_data;
    }

    /**
     * @param $filter
     * @param $newObj
     * @param bool $upsert
     * @param array $options
     * @param WriteConcern|NULL $writeConcern
     * @return \MongoDB\Driver\WriteResult
     */
    public function updateOne($filter, $newObj, bool $upsert = false, array $options = [], WriteConcern $writeConcern = NULL) {
        $enterprise_data = array(
            'enterprise_name' => $newObj['enterprise_name'],//企业名称
            'identifier' => $newObj['identifier'],//统一社会信用代码（Unified Social Credit Identifier）
            'legal_representative' => $newObj['legal_representative'],//法定代表人
            'registered_type' => $newObj['registered_type'],//注册类型
            'registered_capital' => $newObj['registered_capital'],//注册资本
            'registered_date' => $newObj['registered_date'],//成立日期
            'registration_authority' => $newObj['registration_authority'],//登记机关
            'registration_status' => $newObj['registration_status'],//登记状态
            'area' => $newObj['area'],//所在区域
            'registered_address' => $newObj['registered_address'],//注册地址
            'scope_business' => $newObj['scope_business'],//经营范围
            //'create_time' => time(),//录入时间
            //'enterprise_status' => 0,//状态，0：待审核、1：已通过、-1：未通过
            //'logo' => '',//头像
            'contact_info'=>array(
                'contact_name'=>$newObj['contact_name'],//联系人姓名
                'id_card_number'=>$newObj['id_card_number'],//身份证号码
                'phone'=>$newObj['phone'],//联系电话
                'email'=>$newObj['email'],//邮箱
            ),
        );
        return parent::updateOne($filter, array('$set'=>$enterprise_data), $upsert, $options, $writeConcern);
    }

}